﻿$(function(){
	initLeftMenu();
	tabClose();
	tabCloseEven();
});

//初始化左侧
function initLeftMenu() {
	$('.easyui-accordion li a').click(function(){
		$('.easyui-accordion li div').removeClass("selected");
		$(this).parent().addClass("selected");
	}).hover(function(){
		$(this).parent().addClass("hover");
	},function(){
		$(this).parent().removeClass("hover");
	});
}

//获取左侧导航的图标
function getIcon(menuid){
	var icon = 'icon ';
	$.each(_menus.menus, function(i, n) {
		$.each(n.menus, function(j, o) {
		 	if(o.menuid==menuid){
				icon += o.icon;
			}
		});
	});
	return icon;
}

//新增TAB页
function addTab(tabName,url,icon){
    if(!$('#tabs').tabs('exists',tabName)){
    	$('#tabs').tabs('add',{
    		title:tabName,
    		content:createFrame(url),
    		closable:true,
    		icon:icon
    	});
    }else{
    	$('#tabs').tabs('select',tabName);
    	$('#mm-tabupdate').click();
    }
    tabClose();
}

//创建iFrame
function createFrame(url)
{
    var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
    return s;
}

//关闭TAB页
function tabClose()
{
	//双击关闭TAB页
	$(".tabs-inner").dblclick(function(){
		var tabName = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',tabName);
	});
	
	//为选项卡绑定右键
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});

		var tabName =$(this).children(".tabs-closable").text();
		$('#mm').data("currtab",tabName);
		$('#tabs').tabs('select',tabName);
		return false;
	});
}

//关闭当前TAB页
function closeCurrTab(){
	var currTab = $('#tabs').tabs('getSelected');
	var currtab_title = currTab.panel('options').title;
	$('#tabs').tabs('close',currtab_title);
}

//刷新指定TAB页
function refreshTab(tabName){
	if($('#tabs').tabs('exists',tabName)){
		var currTab = $('#tabs').tabs('getTab',tabName);
		var url = $(currTab.panel('options').content).attr('src');
		$('#tabs').tabs('update',{
			tab:currTab,
			options:{
				content:createFrame(url,tabName)
			}
		});
	}
}

//绑定右键菜单事件
function tabCloseEven()
{
	//刷新
	$('#mm-tabupdate').click(function(){
		var currTab = $('#tabs').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		$('#tabs').tabs('update',{
			tab:currTab,
			options:{
				content:createFrame(url)
			}
		});
	});
	
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab_title);
	});
	
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			if (t != '我的主页')
			$('#tabs').tabs('close',t);
		});
	});
	
	//关闭除当前之外的TAB页
	$('#mm-tabcloseother').click(function(){
		$('#mm-tabcloseright').click();
		$('#mm-tabcloseleft').click();
	});
	
	//关闭当前右侧的TAB页
	$('#mm-tabcloseright').click(function(){
		var nextall = $('.tabs-selected').nextAll();
		if(nextall.length==0){
			return false;
		}
		nextall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			if (t != '我的主页')
			$('#tabs').tabs('close',t);
		});
		return false;
	});
	
	//关闭当前左侧的TAB页
	$('#mm-tabcloseleft').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		if(prevall.length==0){
			return false;
		}
		prevall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			if (t != '我的主页')
			$('#tabs').tabs('close',t);
		});
		return false;
	});

	//退出
	$("#mm-exit").click(function(){
		$('#mm').menu('hide');
	});
}

function clock() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();
    var day = now.getDay();
    var hour = now.getHours();
    var minu = now.getMinutes();
    var sec = now.getSeconds();
    var week;
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    week = arr_week[day];
    var time = "";
    time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;
    
    $("#bgclock").html(time);
    
    setTimeout("clock()", 200);
}
